package com.cvte.shop.service.impl;

import com.cvte.csb.core.interfaces.vo.RestResponse;
import com.cvte.csb.web.rest.ResponseFactory;
import com.cvte.shop.constants.UniversalResultFieldsConstants;
import com.cvte.shop.entity.UniversalResultFields;
import com.cvte.shop.entity.UniversalViewConfig;
import com.cvte.shop.entity.dto.ConditionDTO;
import com.cvte.shop.entity.dto.FieldDTO;
import com.cvte.shop.entity.dto.ViewDTO;
import com.cvte.shop.service.UniversalMethodResultService;
import com.cvte.shop.service.UniversalResultFieldsService;
import com.cvte.shop.service.UniversalViewConfigService;
import com.cvte.shop.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cvte.csb.jdbc.mybatis.biz.BaseBiz;
import com.cvte.shop.mapper.UniversalViewConfigMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 服务实现类
 *
 * @author inv
 * @since 2020-08-04
 */
@Service
@Transactional
public class UniversalViewConfigServiceImpl extends BaseBiz<UniversalViewConfigMapper, UniversalViewConfig> implements UniversalViewConfigService {

    @Autowired
    private UniversalViewConfigMapper universalViewConfigMapper;

    @Autowired
    private UniversalResultFieldsService universalResultFieldsService;

    @Autowired
    private UniversalMethodResultService universalMethodResultService;

    @Override
    public RestResponse addViewConfig(ViewDTO viewDTO) {
        List<FieldDTO> fieldDTOList = viewDTO.getFieldDTOList();
        String tableId = viewDTO.getTableId();
        // 生成视图
        String viewId = UUIDUtil.getUUID();
        UniversalViewConfig viewConfig = createView(tableId,viewId);

        // 插入字段
        List<UniversalResultFields> resultFields = universalResultFieldsService.tranfserToFields(fieldDTOList);
        universalResultFieldsService.batchInsertFields(resultFields,tableId,viewId);

        // 初始化一条sql
        universalMethodResultService.createInitViewSql(resultFields,tableId, viewId);

        return ResponseFactory.getOkResponse(null);
    }

    @Override
    public UniversalViewConfig createView(String tableId, String viewId) {
        UniversalViewConfig viewConfig = new UniversalViewConfig();
        viewConfig.setTableId(tableId);
        viewConfig.setViewId(viewId);
        universalViewConfigMapper.insert(viewConfig);
        return viewConfig;
    }



}
